/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package business;

import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;

/**
 *
 * @author stana
 */
public class YahooPlaceFinder {
    private List places;
    private Document doc;
    public List getPlaces(String place)
    {
        SAXBuilder builder=new SAXBuilder();
        place=place.replace(' ', '_');
        String url ="http://where.yahooapis.com/geocode?location="+place+"&appid=123";
        InputSource inputSource;
        try
        {
            inputSource = new InputSource(new InputStreamReader(new URL(url).openStream()));
            doc=builder.build(inputSource);
            parse();
        } catch (Exception ex)
        {
            Logger.getLogger(YahooPlaceFinder.class.getName()).log(Level.SEVERE, null, ex);
        }

        return places;
    }

    private void parse()
    {
        String line1;
        String line2;
        String line3;
        String line4;
        float latitude;
        float longitude;
        places=new ArrayList<Place>();
        List<Element> results=doc.getRootElement().getChildren("Result");
        for(Element i:results)
        {
            Place p=new Place();
            line1=i.getChild("line1").getText();
            line2=i.getChild("line2").getText();
            line3=i.getChild("line3").getText();
            line4=i.getChild("line4").getText();
            latitude=Float.valueOf(i.getChild("latitude").getText());
            longitude=Float.valueOf(i.getChild("longitude").getText());
            p.setLine1(line1);
            p.setLine2(line2);
            p.setLine3(line3);
            p.setLine4(line4);
            p.setLatitude(latitude);
            p.setLongitude(longitude);
            places.add(p);
        }
    }

   


}
